package com.zlsy.learning.leetcode.binarysearch;

/**
 * @author zhouliang
 * @date 2020/10/13 16:23
 */
public class BsearchDemo4 {

  /** 查找第一个大于等于给定值的元素 */
  private static int bsearch(int[] a, int n, int value) {
    int low = 0;
    int high = n - 1;
    while (low <= high) {
      int mid = low + ((high - low) >> 1);
      if (a[mid] >= value) {
        if ((mid == 0) || a[mid - 1] < value) {
          return mid;
        } else {
          high = mid - 1;
        }
      } else {
        low = mid + 1;
      }
    }
    return -1;
  }

  public static void main(String[] args) {
    int[] a = new int[] {3, 4, 6, 7, 10};
    int n = a.length;
    int value = 5;
    System.out.println(bsearch(a, n, value));
  }
}
